package leetcode_动态规划._03数组区间;

/**
 * 等差数列划分
 * 算法：
 *   dp[i] 表示以 A[i] 为结尾的等差子区间的个数
 */
public class _413 {

    public int numberOfArithmeticSlices(int[] nums) {
        int[] dp = new int[nums.length];
        for (int i = 2; i < nums.length; i++) {
            if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
                dp[i] = dp[i - 1] + 1;
            }
        }
        int ans = 0;
        for (int i : dp) {
            ans += i;
        }
        return ans;
    }
    public int numberOfArithmeticSlices_perfect(int[] nums) {
        int ans = 0, tem = 0;
        for (int i = 2; i < nums.length; i++) {
            if (nums[i] - nums[i - 1] == nums[i - 1] - nums[i - 2]) {
                tem = tem + 1;
                ans += tem;
            }
        }
        return ans;
    }
}
